/**
 * Copyright(C) 2012 Luvina Software Company
 *
 * CommonUtil.java, 4/1/2013, NgocAnh
 */
package net.luvina.manageuser.util;

import net.luvina.manageuser.constant.CommonConstant;

/**
 * @author la-pm Ngoc Anh
 *
 */
public class CommonUtil {

    /**
     *
     * get count data need
     *
     * @param currentPage
     *            page current
     * @param limit
     *            record in 1 page
     * @return int data need
     */
    public static int getOffset(int currentPage, int limit) {
        return (currentPage - 1) * limit;
    }

    /**
     * get limit record inpagr
     *
     * @return int limit
     */
    public static int getLimit() {
        return CommonConstant.LIMIT;
    }

    /**
     *
     * pagging for ADM002
     *
     * @param totalRecords
     * @param limit
     * @param currentPage
     * @param actionName
     * @return String pagging
     */
    public static String paging(int totalRecords, int limit, int currentPage,
            String actionName) {
        // int page = getTotalPage(totalRecords, limit);

        int page_range = 3;
        String f_name_js = "abv";
        String separator = "";
        String pre_str = "&lsaquo;";
        String next_str = "&rsaquo;";
        String first_str = "&laquo;";
        String last_str = "&raquo;";

        if (limit == 0) {
            return "";
        }

        String st = "";
        String cc;
        if (actionName.indexOf("?") > -1) {
            cc = "&";
        } else {
            cc = "?";
        }

        int page_num;
        page_num = (int) Math.ceil((double) totalRecords / limit);

        currentPage = (int) currentPage;

        if (currentPage < 1) {
            currentPage = 1;
        }

        if (currentPage > page_num) {
            currentPage = page_num;
        }

        if (page_num < page_range) {
            page_range = page_num;
        }

        int division;
        int page_lim1 = 0;
        int page_lim2 = 0;

        /*
         * if (page_range == 0) { division = 0; } else {
         */
        division = (int) Math.floor(page_num / page_range);
        /* } */

        for (int i = 0; i <= division; i++) {
            if (((i * page_range + 1) <= currentPage)
                    && (((i + 1) * page_range) >= currentPage)) {
                page_lim1 = i * page_range + 1;
                page_lim2 = page_lim1 + page_range - 1;
                if (page_lim2 > page_num) {
                    page_lim2 = page_num;
                }
                break;
            }
        }

        String actionNameEncode = actionName;

        if (page_num > 1) {
            if (currentPage > 1) {
                if (f_name_js.trim().compareTo("") != 0) {
                    if (first_str.trim().compareTo("") != 0)
                        st += " <a href='javascript:" + f_name_js + "(\""
                                + actionNameEncode
                                + "\", 1)'  class='txtBlue'>" + first_str
                                + "</a>";
                    if (pre_str.trim().compareTo("") != 0)
                        st += separator + " <a href='javascript:" + f_name_js
                                + "(\"" + actionNameEncode + "\", "
                                + (currentPage - 1) + ")' class='txtBlue'>"
                                + pre_str + "</a> ";
                } else {
                    if (first_str.trim().compareTo("") != 0)
                        st += " <a href='" + actionName + cc
                                + "page=1&isBack=1' class='txtBlue'>"
                                + first_str + "&nbsp;</a>";
                    if (pre_str.trim().compareTo("") != 0)
                        st += " <a href='" + actionName + cc + "page="
                                + (currentPage - 1)
                                + "&isBack=1' class='txtBlue'>" + pre_str
                                + "</a> ";
                }
            }
        }

        for (int i = page_lim1; i < currentPage; i++) {
            if (f_name_js.trim().compareTo("") != 0) {
                st += separator + "<a href='javascript:" + f_name_js + "(\""
                        + actionNameEncode + "\", " + i + ")' class='txtBlue'>"
                        + i + "</a> ";
            } else {
                st += separator + "<a href='" + actionName + cc + "page=" + i
                        + "&isBack=1' class='txtBlue'>" + i + "</a> ";
            }
        }

        st += separator + "<span class='txtGrey11'>" + currentPage
                + "</span>&nbsp;";

        for (int i = currentPage + 1; i <= page_lim2; i++) {
            if (f_name_js.trim().compareTo("") != 0) {
                st += separator + "<a href='javascript:" + f_name_js + "(\""
                        + actionNameEncode + "\", " + i + ")' class='txtBlue'>"
                        + i + "</a> ";
            } else {
                st += separator + "<a href='" + actionName + cc + "page=" + i
                        + "&isBack=1' class='txtBlue'>" + (i) + "</a> ";
            }
        }
        st += separator;

        if (page_num > 1) {
            if (currentPage + 1 <= page_num) {
                if (f_name_js.trim().compareTo("") != 0) {
                    if (next_str.trim().compareTo("") != 0)
                        st += " <a href='javascript:" + f_name_js + "(\""
                                + actionNameEncode + "\", " + (currentPage + 1)
                                + ")' class='txtBlue'>" + next_str + "</a>";
                    if (last_str.trim().compareTo("") != 0)
                        st += separator + " &nbsp;<a href='javascript:"
                                + f_name_js + "(\"" + actionNameEncode + "\", "
                                + page_num + ")' class='txtBlue'>" + last_str
                                + "</a>";
                } else {
                    if (next_str.trim().compareTo("") != 0)
                        st += " <a href='" + actionName + cc + "page="
                                + (currentPage + 1)
                                + "&isBack=1' class='txtBlue'>" + next_str
                                + "</a>";
                    if (last_str.trim().compareTo("") != 0)
                        st += "  <a href='" + actionName + cc + "page="
                                + page_num + "&isBack=1' class='txtBlue'>"
                                + last_str + "</a>";
                }
            }
        } else {
            st = "";
        }
        return st;
    }

    /**
     * get limit page
     *
     * @param totalRecords
     *            total recors in database
     * @param limit
     *            record in 1 page
     * @return limit page
     */
    public static int getTotalPage(int totalRecords, int limit) {
        if (totalRecords % limit == 0) {
            return totalRecords / limit;
        } else {
            return (totalRecords / limit) + 1;
        }
    }
}
